// Pin set: SCK->D1, SDA->D2
#include "ESP8266WiFi.h"
#include <U8g2lib.h>
// #include <DHT.h>
#include "SHT2x.h"
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

// ESP8266引脚连接
#define OLED_SCL_PIN D1
#define OLED_SDA_PIN D2
// int SHT2X_SDA_PIN = D4;
// int SHT2X_SCL_PIN = OLED_SCL_PIN;

// #define DHTPIN D0
// #define DHTTYPE DHT11
// DHT dht(DHTPIN, DHTTYPE);

#define AQ_BAD D5
#define AQ_MID D6
#define AQ_GOOD D7

#define MQ_135 A0

#define OLED_REF_POS_X  3
#define OLED_REF_POS_Y  0

SHT2x SHT_CTL;

// U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C oled(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);  // 长屏 128X32
U8G2_SSD1306_128X64_NONAME_F_HW_I2C OLED_CTL(U8G2_R0, /* reset=*/U8X8_PIN_NONE); // 方屏 128X64
void setup(void)
{
  Serial.begin(115200);
  Serial.println("****************** Setup start ******************");
  // TwoWire wire;
  // Wire.begin(SHT2X_SDA_PIN, SHT2X_SCL_PIN);
  // SHT2x sht_ctl(&wire);

  Serial.printf("SHT Version:");
  Serial.println(SHT2x_LIB_VERSION);

  OLED_CTL.begin();
  SHT_CTL.begin();

  SHT_CTL.requestTemperature();
  SHT_CTL.requestHumidity();
  
  Serial.printf("SHT Status: %x\n", SHT_CTL.getStatus());

  pinMode(AQ_BAD, OUTPUT);
  pinMode(AQ_MID, OUTPUT);
  pinMode(AQ_GOOD, OUTPUT);

  digitalWrite(AQ_BAD, HIGH);
  digitalWrite(AQ_MID, HIGH);
  digitalWrite(AQ_GOOD, HIGH);
  Serial.println("****************** Setup end *****************");
}

void loop(void)
{
  OLED_CTL.clearBuffer();                 // clear the internal memory
  OLED_CTL.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  Serial.printf("SHT2x read result: %d\n", SHT_CTL.read());

  // if (SHT_CTL.reqTempReady())
  {
    // SHT_CTL.readHumidity();
    float tmp = SHT_CTL.getTemperature();
    Serial.print("温度: ");
    Serial.print(tmp);
    Serial.println("℃");
    char strTmp[64] = {0};
    sprintf(strTmp, "Temperature: %.2f ℃", tmp);
    OLED_CTL.drawStr(OLED_REF_POS_X, OLED_REF_POS_Y + 10, strTmp); // write something to the internal memory
  }

  // if (SHT_CTL.reqHumReady())
  {
    // SHT_CTL.readHumidity();
    float hum = SHT_CTL.getHumidity();
    Serial.print("湿度：");
    Serial.print(hum);
    Serial.println("%");

    char strHum[64] = {0};
    sprintf(strHum, "Humidity: %.1f %", hum);
    OLED_CTL.drawStr(OLED_REF_POS_X, OLED_REF_POS_Y + 30, strHum); // write something to the internal memory
  }

  // if (!SHT_CTL.requestTemperature())
  // {
  //   Serial.println("Request temperature failed.");
  // }
  
  // if (!SHT_CTL.requestHumidity())
  // {
  //   Serial.println("Request humidity failed.");
  // }

  float fMQ = analogRead(MQ_135);
  Serial.print("空气质量：");
  Serial.println(fMQ);
  char strAirLvl[16] = {0};
  if (fMQ <= 80)
  {
    sprintf(strAirLvl, "Great");
    digitalWrite(AQ_BAD, HIGH);
    digitalWrite(AQ_MID, HIGH);
    digitalWrite(AQ_GOOD, LOW);
  }
  else if (fMQ > 80 && fMQ <= 160)
  {
    sprintf(strAirLvl, "Good");
    digitalWrite(AQ_BAD, HIGH);
    digitalWrite(AQ_MID, HIGH);
    digitalWrite(AQ_GOOD, LOW);
  }
  else if (fMQ > 160 && fMQ <= 380)
  {
    sprintf(strAirLvl, "Moderate");
    digitalWrite(AQ_BAD, HIGH);
    digitalWrite(AQ_MID, LOW);
    digitalWrite(AQ_GOOD, HIGH);
  }
  else if (fMQ > 380 && fMQ <= 500)
  {
    sprintf(strAirLvl, "UnHealthy");
    digitalWrite(AQ_BAD, LOW);
    digitalWrite(AQ_MID, HIGH);
    digitalWrite(AQ_GOOD, HIGH);
  }
  else if (fMQ > 500)
  {
    sprintf(strAirLvl, "Hazardous");
    digitalWrite(AQ_BAD, LOW);
    digitalWrite(AQ_MID, HIGH);
    digitalWrite(AQ_GOOD, HIGH);
  }

  char strAirQua[64] = {0};
  sprintf(strAirQua, "Air Quality: %s", strAirLvl);
  OLED_CTL.drawStr(OLED_REF_POS_X, OLED_REF_POS_Y + 50, strAirQua); // write something to the internal memory
  OLED_CTL.sendBuffer();              // transfer internal memory to the display
  delay(10000);
}
